package com.ruoyi.system.constant;


import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.system.constant.MqttPushClient;
import com.ruoyi.system.domain.LogEntry;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author lzj
 */
@Service
public class InstructionService {

    private final MqttPushClient mqttPushClient;
    private static final String hex = "140124013F";
    private final AtomicInteger counter = new AtomicInteger(0);

    @Autowired
    public InstructionService(MqttPushClient mqttPushClient) {
        this.mqttPushClient = mqttPushClient;
    }

    @Async
    public CompletableFuture<Void> executeInstructionsSequence(List<LogEntry> lists) throws InterruptedException, MqttException {
        for (LogEntry entry : lists) {
            String macId = generateMacId();
            boolean res1 = pushB2(macId);
            if (res1) {
                Thread.sleep(1000);
                boolean res2 = pushB4(macId, entry.getIcCard0015(), entry.getIcCard0019(), entry.getEf01Info(), entry.getEf04Info());
                if (res2) {
                    Thread.sleep(3000);
                    boolean res3 = pushB5(macId);
                    if (res3) {
                        Thread.sleep(5000);
                    }
                }
            }
        }
        return CompletableFuture.completedFuture(null);
    }

    private String generateMacId() {
        return "0415P000" + counter.incrementAndGet();
    }

    private boolean pushB2(String macId) throws MqttException {
        String str = String.format("{\"msgId\":\"2024082217000054\",\"pushTime\":\"2024-08-22T17:15:17\",\"msgIndex\":\"112\",\"deviceType\":\"225\",\"deviceIndex\":\"1\",\"deviceFuncType\":\"178\",\"macId\":\"%s\",\"serialNumber\":\"3201622102424294\",\"errorCode\":\"0\",\"issuerIdentifier\":\"BDADCBD532010001\",\"contractType\":\"0\",\"contractVersion\":\"64\",\"dateofIssue\":\"20230531\",\"dateofExpire\":\"20280530\",\"equitmentCV\":\"16\",\"obuStatus\":\"8193\",\"positionStatus\":\"1\",\"obuPosition\":\"FFFFFFDC00000238\",\"antennaID\":\"1\"}", macId);
        mqttPushClient.publish(false, String.format("E/14/G0055/G0055140440/14012401/%s/Push/DevInfo/Rsu", hex), str);
        return true;
    }

    private boolean pushB4(String macId, String str15, String str19, String ef01, String ef04) throws MqttException {
        JSONObject json = new JSONObject();
        String str = String.format("{\"msgId\":\"2025011017000026\",\"pushTime\":\"2025-01-10T17:40:54\",\"msgIndex\":\"128\",\"deviceType\":\"225\",\"deviceIndex\":\"1\",\"deviceFuncType\":\"180\",\"macId\":\"%s\",\"serialNumber\":\"3201622102424294\",\"errorCode\":\"0\",\"authMode\":\"0\",\"eandom\":\"\",\"algorithmID\":\"0\",\"cardStatus\":\"0\",\"cardRestMoney\":\"2147446787\",\"readEF04Status\":\"0\",\"vehicleInfoCostTime\":\"99\",\"cpuCostTime\":\"95\",\"ef04CostTime\":\"50\"}", macId);
        json = JSONObject.parseObject(str);
        json.put("vehicleInfo", ef01);
        json.put("issuerInfo", str15);
        json.put("lastStation", str19);
        json.put("ef04Info", ef04);

        mqttPushClient.publish(false, String.format("E/14/G0055/G0055140440/14012401/%s/Push/DevInfo/Rsu", hex), json.toString());

        return true;
    }

    private boolean pushB5(String macId) throws MqttException {
        String str = String.format("{\"msgId\":\"2024082217000056\",\"pushTime\":\"2024-08-22T17:15:18\",\"msgIndex\":\"144\",\"deviceType\":\"225\",\"deviceIndex\":\"1\",\"deviceFuncType\":\"181\",\"macId\":\"%s\",\"serialNumber\":\"3201622102424294\",\"errorCode\":\"0\",\"psamNo\":\"123456789ABC\",\"transTime\":\"20240822171517\",\"transType\":\"9\",\"tac\":\"A6CEFAF6\",\"iccPaySerial\":\"39\",\"psamTransSerial\":\"1021212\",\"cardBalance\":\"2147446786\",\"algorithmId\":\"0\",\"mac2\":\"44430D06\",\"keyVersion\":\"1\",\"writeEF04Status\":\"0\"}", macId);
        mqttPushClient.publish(false, String.format("E/14/G0055/G0055140440/14012401/%s/Push/DevInfo/Rsu", hex), str);
        return true;
    }
}
